package com.nkzy.controller;

import com.nkzy.common.Result;
import com.nkzy.pojo.Article;
import com.nkzy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/{id}")
    public Result<Article> getArticleById(@PathVariable Integer id) {
        Article article = articleService.selectById(id);
        if (article != null) {
            return Result.success(article);
        } else {
            return Result.error("Article not found");
        }
    }

    @GetMapping
    public Result<List<Article>> getAllArticles() {
        List<Article> articles = articleService.selectAll();
        return Result.success(articles);
    }

    @PostMapping
    public Result<Void> addArticle(@RequestBody Article article) {
        articleService.insert(article);
        return Result.success(null);
    }

    @PutMapping
    public Result<Void> updateArticle(@RequestBody Article article) {
        articleService.update(article);
        return Result.success(null);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable Integer id) {
        articleService.deleteById(id);
        return Result.success(null);
    }
}